﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using GSQ.BusinessEntities;
using GSQ.DataAccess.DataSetTableAdapters;

namespace GSQ.DataAccess
{
    public class VendedoresDA
    {
        private VENDEDORESTableAdapter Adapter = new VENDEDORESTableAdapter();
        private DataSet.VENDEDORESDataTable entities;
        private DataSet.VENDEDORESRow entity;

        public decimal Insert(VENDEDORES value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    Adapter.Connection = cnn;

                    entities = new DataSet.VENDEDORESDataTable();

                    entity = entities.NewVENDEDORESRow();

                    entity.ID = value.ID;
                    entity.VENDEDOR = value.VENDEDOR.Trim();
                    entity.EXCENTO_VENTAS = value.EXCENTO_VENTAS;
                    entity.EXCENTO_COBROS = value.EXCENTO_COBROS;
                    entity.X_VENTAS = value.X_VENTAS;
                    entity.X_COBROS = value.X_COBROS;
                    entity.ACTIVO = value.ACTIVO;

                    entities.AddVENDEDORESRow(entity);
                    Adapter.Update(entities);

                    value.ID = entity.ID;
                    return value.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public decimal Update(VENDEDORES value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    entities = Adapter.GetDataByID(value.ID);
                    entity = entities.FirstOrDefault();


                    entity.VENDEDOR = value.VENDEDOR.Trim();
                    entity.EXCENTO_VENTAS = value.EXCENTO_VENTAS;
                    entity.EXCENTO_COBROS = value.EXCENTO_COBROS;
                    entity.X_VENTAS = value.X_VENTAS;
                    entity.X_COBROS = value.X_COBROS;
                    entity.ACTIVO = value.ACTIVO;

                    this.Adapter.Update(entities);

                    return entity.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public VENDEDORES GetById(decimal id)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    VENDEDORES vendedor;

                    entity = this.Adapter.GetDataByID(id).FirstOrDefault();

                    if (entity != null)
                    {
                        vendedor = new VENDEDORES
                            {
                                ID = entity.ID,
                                VENDEDOR = entity.VENDEDOR,
                                EXCENTO_COBROS = entity.EXCENTO_COBROS,
                                EXCENTO_VENTAS= entity.EXCENTO_VENTAS,
                                X_COBROS= entity.X_COBROS,
                                X_VENTAS= entity.X_VENTAS,
                                ACTIVO = entity.ACTIVO
                            };
                    }
                    else
                        vendedor = null;

                    return vendedor;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<VENDEDORES> GetAll()
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<VENDEDORES> lst = new List<VENDEDORES>();
                    foreach (DataSet.VENDEDORESRow rw in this.Adapter.GetData())
                    {
                        lst.Add(new VENDEDORES
                        {
                            ID = rw.ID,
                            VENDEDOR = rw.VENDEDOR,
                            EXCENTO_COBROS = rw.EXCENTO_COBROS,
                            EXCENTO_VENTAS = rw.EXCENTO_VENTAS,
                            X_COBROS = rw.X_COBROS,
                            X_VENTAS = rw.X_VENTAS,
                            ACTIVO = rw.ACTIVO
                        });
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<VENDEDORES> GetByVendedor(string value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<VENDEDORES> lst = new List<VENDEDORES>();
                    foreach (DataSet.VENDEDORESRow rw in this.Adapter.GetDataByVendedor(value.Trim()))
                    {
                        lst.Add(new VENDEDORES
                        {
                            ID = rw.ID,
                            VENDEDOR = rw.VENDEDOR,
                            EXCENTO_COBROS = rw.EXCENTO_COBROS,
                            EXCENTO_VENTAS = rw.EXCENTO_VENTAS,
                            X_COBROS = rw.X_COBROS,
                            X_VENTAS = rw.X_VENTAS,
                            ACTIVO = rw.ACTIVO
                        });
                    }
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }
    }
}
